package com.lee.datastructures.linkedlist;

import java.util.Stack;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2022/4/3
 */
public class ReverseList {
    public static void main(String[] args) {
        Node head = new Node(new Node(new Node(new Node(new Node(null, "5"), "4"), "3"), "2"), "1");
//        do {
//            head.print();
//            head = head.getNext();
//        } while (head != null);

        Node reverse = reverse(head);


        do {
            reverse.print();
            reverse = reverse.getNext();
        } while (reverse != null);
    }

    private static Node reverse(Node head) {
        Stack<Node> stack = new Stack<>();
        do {
            stack.push(head);
            head = head.getNext();
        } while (head != null);

        head = stack.pop();
        Node temp = head;
        while (!stack.empty()) {
            final Node pop = stack.pop();
            temp.setNext(pop);
            pop.setNext(null);
            temp = pop;
        }
        return head;
    }

    private static Node reverse2(Node head) {
        Node cur=head;
        Node prev=null;
        while (cur == null) {
            cur.setNext(prev);
            prev=cur;
            cur=cur.getNext();
        }
        return cur;
    }
}
